<template>
	<sw-common-frame :count="dataCount" :loading="loading">
		<template slot="button" v-if="isExistButton">
			<el-button size="small" type="primary" v-hasPermi="addedButton" icon="el-icon-plus" @click="handleOpenDialog()">新增</el-button>
		</template>
		<el-form slot="query" size="small" inline v-if="isExistQuery">
			<el-form-item><el-input v-model="queryData.title" placeholder="请输入标题" clearable /></el-form-item>
			<el-form-item><el-button type="primary" icon="el-icon-search" @click="handleQueryTableData()">查询</el-button></el-form-item>
		</el-form>
		<el-table slot="table" v-bind="tableConfig" :data="tableData">
			<el-table-column label="序号" type="index" width="50" align="center" />
			<el-table-column label="标题" prop="title" min-width="240">
				<template slot-scope="{ row }">
					<template v-if="!isHasDetail">{{ row.title }}</template>
					<router-link v-else :to="`/system/notice/${row.id}`">{{ row.title }}</router-link>
				</template>
			</el-table-column>
			<el-table-column label="发布时间" min-width="140" :formatter="returnReleaseTime" />
			<el-table-column label="创建者" prop="createName" min-width="120"></el-table-column>
			<el-table-column label="创建时间" prop="createTime" min-width="160"></el-table-column>
			<el-table-column label="操作" fixed="right" :width="handleWidth" v-if="isHasAuthButton">
				<template slot-scope="{ row }">
					<template v-if="row.del === '1'">
						<el-button type="text" v-hasPermi="handleButtons.status" @click="handlePublishData(row, 2)">下架</el-button>
					</template>
					<template v-if="row.del === '2'">
						<el-button type="text" v-hasPermi="handleButtons.status" @click="handlePublishData(row, 1)">发布</el-button>
					</template>
					<el-button type="text" v-hasPermi="handleButtons.update" @click="handleOpenDialog(row.id)" :disabled="row.del !== '2'">编辑</el-button>
					<el-button type="text" v-hasPermi="handleButtons.delete" @click="handleDeleteData(row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<sw-pagination slot="page" :total="dataCount" :page.sync="queryData.page" :limit.sync="queryData.limit" @change="queryTableData" />
		<form-dialog v-model="formDialog" :data-id="dataId" @refresh="queryTableData()"></form-dialog>
	</sw-common-frame>
</template>
<script>
import lodash from 'lodash';
import moment from 'moment';
import formDialog from './form';
import listMixins from '@/mixins/listMixins';
import { queryJournalismList, deleteJournalism } from '@/api/system/tbSysJournalism';
import { updateJournalismDel, updateJournalismSort, updateJournalismTop } from '@/api/system/tbSysJournalism';
export default {
	name: 'noticePage',
	mixins: [listMixins],
	components: { formDialog },
	data() {
		return {
			pageName: '通知公告'
		};
	},
	computed: {
		listFunction() {
			return eval(queryJournalismList);
		},
		deleteFunction() {
			return eval(deleteJournalism);
		},
		frameTopButton() {
			let frameTopBtns = [];
			if (!!this.addedButton) {
				frameTopBtns = [...frameTopBtns, this.addedButton];
			}
			return frameTopBtns;
		},
		isHasDetail() {
			let detail = lodash.find(this.buttons, { field: 'detail' });
			return lodash.some(this.authButton, { id: detail?.id });
		}
	},
	methods: {
		//返回发布时间
		returnReleaseTime(row, column, cellValue, index) {
			return moment(row.releaseTime).format('YYYY-MM-DD');
		},

		//发布/下架公告
		async handlePublishData(data, del) {
			let params = { id: data.id, del };
			let result = await updateJournalismDel(params).then((res) => res.data);
			if (result.code === '0') {
				if (del === 1) {
					this.$showCustomMessage('success', `发布公告成功`);
				} else if (del === 2) {
					this.$showCustomMessage('success', `下架公告成功`);
				}
				this.queryTableData();
			}
		}
	}
};
</script>
<style lang="scss" scoped>
.announcement {
}
</style>
